
function LinkedList() {
    var Node = function (val) {
        this.val = val
        this.next = null
    }
    var length = 0
    var head = null
    this.append = function (val) {
        var node = new Node(val);
        var current;
        if (head === null) {
            head = node
        } else {
            current = head
            while (current.next) {
                current = current.next
            }
            current.next = node
        }
        length++
    };
    this.toString = function () {
        var string = head.val
        var current = head.next
        while (current) {
            string += ',' + current.val
            current = current.next
        }
        return string
    }
    this.reverse = function () {
        var list = head
        var p = list, q = null
        while (p.next != null) {
            q = p.next
            p.next = q.next
            q.next = list
            list = q
        }
        head = list
        return list
    }
}
var li = new LinkedList();
li.append(1);
li.append(2);
li.append(3);
li.append(4);
console.log(li.toString());
console.log(li.reverse());
console.log(li.toString());
